Skip to content

Conversation

@shjwudp
Copy link
Contributor

@shjwudp shjwudp commented Dec 23, 2025

What does this PR do ?

main branch: #3076
NOTE: The kernel will be migrated to TE in future updates, and the main branch will depend on this version of the kernel.

1. Fix: incorrect parameter reference in Linear-Cross-Entropy Fusion for non-shared embeddings

When using non-shared embeddings, Linear-Cross-Entropy Fusion incorrectly references the shared embedding weight. In this scenario, the return value of shared_embedding_or_output_weight should not be used. Instead, it should explicitly reference output_layer.weight.

Additionally, since Linear-Cross-Entropy Fusion does not invoke the output_layer module, m-core DDP’s parameter gathering check may fail. To address this, I subclassed output_layer (i.e., ColumnParallelLinear) and moved the Linear-Cross-Entropy logic into its forward method.

2. Improvement: Use FP32 accumulation in multi-chunk computation for better convergence

The previous implementation of Linear-Cross-Entropy Fusion split computation along the vocabulary dimension and accumulated gradients in BF16, using the operation:

d_hidden += d_logits_slice * weight_slice

This caused d_hidden to accumulate in BF16, which could lead to reduced numerical accuracy.

This PR fixes the issue by allocating an FP32 buffer for d_hidden during accumulation, then downcasting it back to BF16 after accumulation completes. This improves numerical stability and overall convergence.

Contribution process

flowchart LR
    A[Pre-checks] --> B[PR Tests]
    subgraph Code Review/Approval
        C1[Expert Review] --> C2[Final Review]
    end
    B --> C1
    C2 --> D[Merge]
Loading

Pre-checks

  • I want this PR in a versioned release and have added the appropriate Milestone (e.g., Core 0.8)
  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

The following process is enforced via the CODEOWNERS file for changes into megatron/core. For changes outside of megatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.

For MRs into `main` branch

(Step 1): Add PR label Expert Review

(Step 2): Collect the expert reviewers reviews

  1. Attach the Expert Review label when your PR is ready for review.
  2. GitHub auto-assigns expert reviewers based on your changes. They will get notified and pick up your PR soon.

⚠️ Only proceed to the next step once all reviewers have approved, merge-conflict are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

(Step 3): Final Review

  1. Add Final Review label
  2. GitHub auto-assigns final reviewers based on your changes. They will get notified and pick up your PR soon.

(Optional Step 4): Cherry-pick into release branch

If this PR also needs to be merged into core_r* release branches, after this PR has been merged, select Cherry-pick to open a new PR into the release branch.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either [email protected] or [email protected].

Merging your PR

Any member of core-adlr and core-nemo will be able to merge your PR.

@shjwudp shjwudp requested review from a team as code owners December 23, 2025 05:44
@copy-pr-bot
Copy link

copy-pr-bot bot commented Dec 23, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@shjwudp shjwudp self-assigned this Dec 24, 2025
@shjwudp shjwudp added the Expert Review Apply this label to indicate that your PR is ready for expert review. label Dec 24, 2025
@shjwudp
Copy link
Contributor Author

shjwudp commented Dec 24, 2025

/ok to test 3cae089

Copy link

@Jianbing-D Jianbing-D left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kernel modifications looks good to me

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 21, 2026

/ok to test 483c1a8

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 21, 2026

/ok to test 22f770f

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 22, 2026

/ok to test 0217dd6

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 22, 2026

Hi @NVIDIA/core-devtech @NVIDIA/core-nemo , could you help review this PR? Jianbing has reviewed and approved the changes to the kernel.

@shjwudp shjwudp requested a review from a team January 22, 2026 08:13
@lhb8125 lhb8125 changed the title Fix Linear-Cross-Entropy Convergence Issue [Dev] Fix Linear-Cross-Entropy Convergence Issue Jan 22, 2026
Copy link
Contributor

@lhb8125 lhb8125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!
@ISEEKYAN Could you also review this PR so that it meets the requirement for RL?

@lhb8125 lhb8125 requested a review from ISEEKYAN January 22, 2026 10:35
Copy link
Contributor

@ISEEKYAN ISEEKYAN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is OK for RL worker now. We can set reduction=None to get log_probs which is the necessary output for RL. It is better to enable this kernel with ability of giving entropy along with log_prob, we can do this in a new PR. cc @HaochenYuan

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 23, 2026

/ok to test 5fd23e6

@shjwudp
Copy link
Contributor Author

shjwudp commented Jan 23, 2026

/ok to test 2d2c30c

@yanring
Copy link
Contributor

yanring commented Jan 26, 2026

Hi Jack, could you please add the link to the main PR in the desc? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dev branch Dev branch related issues and development Expert Review Apply this label to indicate that your PR is ready for expert review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants